package com.wuuxiang.polestar.demo.java;

import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

import com.google.common.collect.Maps;
import com.wuuxiang.polestar.core.zookeeper.surpass.ZookeeperSurPassConfigGroup;
import com.wuuxiang.polestar.core.zookeeper.surpass.ZookeeperSurPassConfigProfile;
import com.wuuxiang.polestar.demo.util.ZookeeperServer;

/**********************************************************
 * 类名称：PolestarJavaDemo <br>
 * 类描述： <br>
 * 创建人：liuwei <br>
 * 创建时间：2017年12月22日下午2:13:09 <br>
 * 修改备注： <br>
 **********************************************************/
public class PolestarJavaDemo {

	static {
		try {
			int port = 2180;
			ZookeeperServer.start(port);
			Map<String, String> data = Maps.newHashMap();
			data.put("/polestar/config/crm6/comm-conf/test1", "1");
			data.put("/polestar/config/crm6/comm-conf/test2", "2");
			data.put("/polestar/config/crm6/comm-conf/test3", "3");
			data.put("/polestar/config/crm6/comm-conf/test4", "4");
			data.put("/polestar/config/crm6/comm-conf/defaultTest", "666");
			data.put("/polestar/config/default/comm-conf/defaultTest", "5");
			CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:" + port,
					new ExponentialBackoffRetry(100, 2));
			client.start();
			for (Entry<String, String> item : data.entrySet()) {
				Stat stat = client.checkExists().forPath(item.getKey());
				if (stat == null) {
					client.create().creatingParentsIfNeeded().forPath(item.getKey(), item.getValue().getBytes());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		PolestarJavaDemo demo = new PolestarJavaDemo();
		demo.test1();
	}

	public void test1() {
		ZookeeperSurPassConfigProfile profile = new ZookeeperSurPassConfigProfile("127.0.0.1:2180", "crm6", true);
		ZookeeperSurPassConfigGroup zkConfigGroup = new ZookeeperSurPassConfigGroup(profile, "comm-conf");
		Object obj = "defaultTest";
		System.out.println(zkConfigGroup.get(obj));
		try {
			zkConfigGroup.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void test2() {
		ZookeeperSurPassConfigProfile profile = new ZookeeperSurPassConfigProfile("127.0.0.1:2180", "crm6", false);
		ZookeeperSurPassConfigGroup zkConfigGroup = new ZookeeperSurPassConfigGroup(profile, "comm-conf");
		Object obj = "defaultTest";
		System.out.println(zkConfigGroup.get(obj));
		try {
			zkConfigGroup.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
